<template>
  <div>
    <van-nav-bar title="编辑页面" left-arrow @click-left="$router.go(-1)" />
    <div class="avatar">
      <van-image
        width="120"
        height="120"
        round
        fit="cover"
        :src="userInfo.photo"
      />
      <!-- 上传文件 -->
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group>
      <van-cell
        title="名称"
        :value="userInfo.name"
        is-link
        @click="showNameClick"
      />
      <van-cell
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        is-link
        @click="showGender = true"
      />
      <van-cell
        title="生日"
        :value="userInfo.birthday"
        is-link
        @click="birthdayClick"
      />
    </van-cell-group>

    <!-- 昵称弹窗 -->
    <van-dialog
      v-model="showName"
      title="修改昵称"
      show-cancel-button
      @confirm="editName"
    >
      <van-cell-group>
        <van-field v-model="newName" placeholder="请输入昵称" ref="input" />
      </van-cell-group>
    </van-dialog>

    <!-- 性别弹窗 -->
    <van-popup v-model="showGender" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-arrow
        @click-left="showGender = false"
      />
      <van-cell title="男" is-link @click="editGender(0)" />
      <van-cell title="女" is-link @click="editGender(1)" />
    </van-popup>

    <!-- 修改生日 -->
    <van-popup v-model="showBirthday" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showBirthday = false"
        @confirm="editBirthday"
      />
    </van-popup>

    <!-- 裁切组件 -->
    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120"
        autoCropHeight="120"
      ></vueCropper>
      <van-button type="default" class="cancel" @click="showCropper = false"
        >取消</van-button
      >
      <van-button type="primary" class="confirm" @click="editCropper"
        >确认</van-button
      >
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { editPhoto, editUserInfo } from './user'
import * as moment from 'moment'
import { VueCropper } from 'vue-cropper'
export default {
  data () {
    return {
      showName: false,
      newName: '',
      showGender: false,
      showBirthday: false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      img: null,
      showCropper: false
    }
  },
  methods: {
    showNameClick () {
      this.showName = true
      this.newName = this.userInfo.name
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    // 编辑昵称
    editName () {
      editUserInfo({ name: this.newName }).then(res => {
        // console.log(res);
        this.$store.commit('updateUserInfo', {
          ...this.userInfo,
          name: this.newName
        })
        this.$toast.success('更新成功')
      })
    },
    // 编辑性别
    editGender (gender) {
      editUserInfo({ gender }).then(res => {
        this.showGender = false
        this.$store.commit('updateUserInfo', {
          ...this.userInfo,
          gender
        })
        this.$toast.success('更新成功')
      })
    },
    // 编辑生日
    birthdayClick () {
      this.showBirthday = true
      this.currentDate = new Date(this.userInfo.birthday)
    },
    editBirthday () {
      let birthday = moment(this.currentDate).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(res => {
        this.$store.commit('updateUserInfo', {
          ...this.userInfo,
          birthday
        })
        this.$toast.success('更新成功')
        this.showBirthday = false
      })
    },
    // 上传文件
    afterRead (file) {
      this.img = file.content
      this.showCropper = true
      // 此时可以自行将文件上传至服务器
      // let fd = new FormData()
      // fd.append('photo', file.file)
      // editPhoto(fd).then(res => {
      //   // console.log(res);
      //   this.$store.commit('updateUserInfo', {
      //     ...this.userInfo,
      //     photo: res.data.data.photo
      //   })
      // })
    },
    editCropper () {
      this.$refs.cropper.getCropBlob(data => {
        let fd = new FormData()
        fd.append('photo', data)
        editPhoto(fd).then(res => {
          // console.log(res);
          this.$store.commit('updateUserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
          this.showCropper = false
        })
      })
    }
  },
  computed: {
    ...mapState(['userInfo'])
  },
  components: {
    VueCropper
  }
}
</script>

<style lang="less">
.avatar {
  height: 160px;
  display: flex;
  justify-content: center;
  align-items: center;

  .van-uploader {
    position: absolute;
    width: 125px;
    height: 125px;
    opacity: 0;
    .van-uploader__upload {
      width: 125px;
      height: 125px;
    }
  }
}
.cropper-box {
  position: absolute;
  left: 0;
  bottom: 0;
  top: 0;
  right: 0;
  .cancel {
    position: absolute;
    left: 5px;
    bottom: 5px;
  }
  .confirm {
    position: absolute;
    right: 5px;
    bottom: 5px;
  }
}
</style>
